/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.Color;
import java.awt.Rectangle;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import model.Baum;
import model.Kante;
import model.Knoten;
import model.Pfad;
import model.Tabelle;
import org.netbeans.api.visual.anchor.Anchor;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author ms0022
 */
public class TreeSceneErstellerRegel extends GraphScene<Knoten, Kante> {
    
    protected Pfad pfad;
    protected LayerWidget mainLayer;
    protected LayerWidget connectionLayer;
    protected Baum baum;
    protected ArrayList<Knoten> arrayKnoten;
    protected ArrayList<Kante> arrayKanten;
    protected Knoten wurzel;
    
    public TreeSceneErstellerRegel(Baum baum, Pfad pfad) {
        this.pfad = pfad;
        this.baum = baum;  
        mainLayer = new LayerWidget(this);
        connectionLayer = new LayerWidget(this);
        addChild(mainLayer);
        addChild(connectionLayer);
        arrayKnoten = baum.getKnoten();
        arrayKanten = baum.getKanten();
        erzeugeBaum();
    }
    
     @Override
    protected Widget attachEdgeWidget(Kante e) {
        ConnectionWidget cW = new ConnectionWidget(this);
        cW.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        
        //Beschriftung der Kante 
        LabelWidget beschriftung = new LabelWidget(this, e.getLabel());
        beschriftung.setOpaque (true);
        //label2.getActions ().addAction (ActionFactory.createMoveAction ());
        cW.addChild (beschriftung);
        if (pfad.getKantenListe().contains(e)) {
            cW.setLineColor(Color.BLUE);
        } else {
            cW.setLineColor(Color.LIGHT_GRAY);
        }
        cW.setConstraint (beschriftung, LayoutFactory.ConnectionWidgetLayoutAlignment.CENTER, 0.5f);
        cW.setToolTipText(beschriftung.getLabel());
        //label2.getActions ().addAction (action);

        connectionLayer.addChild(cW);
        return cW;
    }
     public Knoten getWurzel() {
        return arrayKnoten.get(0);
    }
    
    public  Rectangle getGroesse(){
        return this.getBounds();
    }

    public void erzeugeBaum() {
        erzeugeKnoten();
        erzeugeKanten();
    }
    
    public void erzeugeKnoten(){
        //Alle Knoten erzeugen und auf die Scene packen
        for (int i = 0; i <= arrayKnoten.size() - 1; i++) {
            addNode(arrayKnoten.get(i));
        }
    }

    public void erzeugeKanten() {
         for (int i=0; i<=arrayKanten.size()-1; i++){
             fuegeKanteEin(arrayKanten.get(i));
         }
    }

    public void fuegeKanteEin(Kante kante) {
        addEdge(kante);
        //Startpunkt der Kante 
        setEdgeSource(kante, kante.getStartKnoten());
        //ENdpunkt der Kante
        setEdgeTarget(kante, kante.getEndKnoten());    
    }

    @Override
    protected Widget attachNodeWidget(Knoten n) {
        LabelWidget w = new LabelWidget(this);
        if (pfad.getKnotenListe().contains(n)) {
            w.setBorder(BorderFactory.createLineBorder(Color.blue));
        } else {
            w.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        }
        w.setLabel(" " + n.getLabel() + " ");
        w.setToolTipText("Entropie: " + n.getEntropie() + "\n");
        int objektAnz=0;
        for (int i=0; i < n.getTupelListe().size(); i++) {
            w.setToolTipText(w.getToolTipText() + "Klasse: " + n.getTupelListe().get(i).getDaten()[0] + " Häufigkeit: " + n.getTupelListe().get(i).getHaeufigkeit() + "\n");
            objektAnz+=n.getTupelListe().get(i).getHaeufigkeit();
        }
        w.setToolTipText(w.getToolTipText() + "Gesamtanzahl an Objekten: " + objektAnz);
        mainLayer.addChild(w);
        return w;
    }

    @Override
    protected void attachEdgeSourceAnchor(Kante e, Knoten n, Knoten n1) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        ConnectionWidget edgeWidget = (ConnectionWidget) findWidget(e);
        Widget sourceNodeWidget = findWidget(n1);
        Anchor sourceAnchor = AnchorFactory
                .createRectangularAnchor(sourceNodeWidget);
        edgeWidget.setSourceAnchor(sourceAnchor);
    }

    @Override
    protected void attachEdgeTargetAnchor(Kante e, Knoten n, Knoten n1) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        ConnectionWidget edgeWidget = (ConnectionWidget) findWidget(e);
        Widget targetNodeWidget = findWidget(n1);
        Anchor targetAnchor = AnchorFactory
                .createRectangularAnchor(targetNodeWidget);
        edgeWidget.setTargetAnchor(targetAnchor);
    }

    /**
     * @param pfad the pfad to set
     */
    public void setPfad(Pfad pfad) {
        this.pfad = pfad;
    }
    
}
